﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarBlackBox
{
    public partial class SerialCommunication
    {
        private RS485Result Receive(ushort needLen,ref byte[] RxList)
        {
            string strRx;
            ushort RxCount = 0;
            int retry = Properties.Settings.Default.SerialPortErrorBytes;
            while (retry != 0)
            {
                try
                {
                    RxList[RxCount++] = (byte)_COM.ReadByte();
#if DEBUG
                    strRx = RxList[RxCount - 1].ToString("X2")+" ";                    
                    CommunicationDebugForm.ExternShowRx(strRx);
#endif
                }
                catch/* (TimeoutException ex)*/
                {
                    WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                    log.LogType = LogType.串口通信.ToString();
                    log.LogContent = _COM.PortName.ToString() + ":设定系统时间失败";
                    log.LogRemark = "通信超时";
                    log.InserSQLLog();
                    MessageBox.Show("通信失败，记录仪没有回应!!");
                    return RS485Result.Timeout;
                }
                if (RxCount == 1)
                {
                    if (RxList[0] != CommunicationFrame.RxHeader1)
                    {
                        RxCount = 0;
                        retry--;
                    }
                }
                else if (RxCount == 2)
                {
                    if (RxList[1] != CommunicationFrame.RxHeader2)
                    {
                        RxCount = 0;
                        retry--;
                    }
                }
                else if (RxCount == 5)//应该已经收到了长度
                {
                    if (RxList[2] == 0xfa)//当前帧为出错帧
                    {
                        if (WFNetLib.Verify.GetVerify_byteXOR(RxList, RxCount - 1) == RxList[RxCount - 1])//校验正确表示当前确实为错误帧
                        {
                            retry = 0;
                            break;
                        }
                        else//当前数据接收错误,并未是正确的错误帧
                        {
                            RxCount = 0;
                            retry--;
                        }
                    }
                    else if (needLen != WFNetLib.BytesOP.MakeShort(RxList[3], RxList[4]))
                    {
                        RxCount--;
                        retry--;
                    }
                    else
                        needLen++;//算上备用字节
                }
                else if (RxCount > 5)//判断是否接收完成
                {
                    if (needLen == 0)
                    {
                        break;
                    }
                    else
                        needLen--;
                }
            }
            if (retry == 0)
            {
                MessageBox.Show("通信失败，记录仪回应数据有误!!");
                return RS485Result.Error;
            }
            byte verify = WFNetLib.Verify.GetVerify_byteXOR(RxList, RxCount - 1);
            if (verify != RxList[RxCount - 1])//校验不正确
            {
                MessageBox.Show("通信失败，记录仪回应数据校验有误!!");
                return RS485Result.Error;
            }
            return RS485Result.OK;
        }
    }
}
